const NewsService = require("../../services/admin/NewsService")

const NewsController = {
  add:async (req,res) => {
    const cover = req.file ? `/newsuploads/${req.file.filename}` : ''
    const { title,content,category,isPublish } = req.body
      await NewsService.add({title,content,category,cover,editTime:new Date(),isPublish:Number(isPublish)})
      res.send({
        ActionType:'OK'
      })
  },
  updateList:async (req,res) => {
    const cover = req.file ? `/newsuploads/${req.file.filename}` : ''
    const { title,content,category,isPublish,_id } = req.body
      await NewsService.updateList({_id,title,content,category,cover,editTime:new Date(),isPublish:Number(isPublish)})
      res.send({
        ActionType:'OK'
      })
  },
  getList:async (req,res) => {
    // 获取列表的接口要将数据返回出去
    const result = await NewsService.getList(req.params.id)
    res.send({
      ActionType:'OK',
      data:result
    })
  },
  publish:async (req,res) => {
    await NewsService.publish({...req.body,editTime:new Date()})
    res.send({
      ActionType:'OK'
    })
  },
  delList:async (req,res) => {
    // 这里req.params.id是因为占位符后接的是:id
    await NewsService.delList({_id:req.params.id})
    res.send({
      ActionType:'OK'
    })
  } 
}

module.exports = NewsController